package com.kernelstudio.framework.boot.autoconfigure.framework.compatible;
/*
 * This file is part of the kernelstudio package.
 *
 * (c) 2014-2024 zlin <admin@kernelstudio.com>
 *
 * For the full copyright and license information, please view the LICENSE file
 * that was distributed with this source code.
 */

import com.kernelstudio.framework.boot.condition.data.ConditionalOnSpringDataJdbc;
import com.kernelstudio.framework.boot.condition.framework.compatible.ConditionalOnCompatible;
import com.kernelstudio.framework.boot.condition.framework.compatible.ConditionalOnLiquibaseCompatible;
import com.kernelstudio.framework.boot.condition.jdbc.ConditionalOnKingBase8dbcDriver;
import com.kernelstudio.framework.boot.properties.framework.compatible.FrameworkDatabaseCompatibleProperties;
import com.kernelstudio.framework.compatible.configurer.CompatibleConfigurer;
import com.kernelstudio.framework.compatible.supports.jdbc.CompatibleJdbcDialectProviderConfigurer;
import com.kernelstudio.framework.compatible.supports.liquibase.database.LiquibaseKingBase8DatabaseCompatibleConfigurer;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConditionalOnCompatible
@EnableConfigurationProperties({
        FrameworkDatabaseCompatibleProperties.class
})
public class FrameworkCompatibleDatabaseAutoConfiguration {

    @Configuration
    @ConditionalOnCompatible
    @EnableConfigurationProperties({
            FrameworkDatabaseCompatibleProperties.class
    })
    public static class FrameworkCompatibleConfigurersAutoConfiguration {

        private final List<CompatibleConfigurer> compatibleConfigurers;

        private final FrameworkDatabaseCompatibleProperties databaseCompatibleProperties;

        public FrameworkCompatibleConfigurersAutoConfiguration(
                List<CompatibleConfigurer> compatibleConfigurers,
                FrameworkDatabaseCompatibleProperties databaseCompatibleProperties) {
            this.compatibleConfigurers = compatibleConfigurers;
            this.databaseCompatibleProperties = databaseCompatibleProperties;
        }

        @PostConstruct
        public void postCompatibleConfigurers() {
            for (CompatibleConfigurer compatibleConfigurer : compatibleConfigurers) {
                compatibleConfigurer.configure(databaseCompatibleProperties.getMode());
            }
        }
    }

    /**
     * 兼容处理spring data jdbc
     */
    @Configuration
    @ConditionalOnCompatible
    @ConditionalOnSpringDataJdbc
    public static class FrameworkCompatibleJdbcAutoConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public CompatibleJdbcDialectProviderConfigurer compatibleJdbcDialectProviderConfigurer() {
            return new CompatibleJdbcDialectProviderConfigurer();
        }
    }

    /**
     * 人大金仓数据库
     */
    @Configuration
    @ConditionalOnKingBase8dbcDriver
    @ConditionalOnLiquibaseCompatible
    public static class FrameworkCompatibleLiquibaseKingBase8AutoConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public LiquibaseKingBase8DatabaseCompatibleConfigurer liquibaseKingBase8DatabaseCompatibleConfigurer() {
            return new LiquibaseKingBase8DatabaseCompatibleConfigurer();
        }
    }

}
